package com.thuanpq60023.easyenglish.adapter;

import com.thuanpq60023.easyenglish.entity.VocabEntity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class VocabularyAdapter {
	private DatabaseAdapter mDbAdapter;

	public VocabularyAdapter(Context context) {
		mDbAdapter = new DatabaseAdapter(context);
	}

	public boolean insertVocab(VocabEntity entity) {
		if (entity != null) {
			ContentValues inititalValues = new ContentValues();
			inititalValues.put(VocabEntity.WORD, entity.getWord());
			inititalValues.put(VocabEntity.MEAN, entity.getMean());
			inititalValues.put(VocabEntity.TYPE, entity.getType());
			inititalValues.put(VocabEntity.EXAMPLE, entity.getExample());
			inititalValues.put(VocabEntity.UNIT, entity.getUnit());
			inititalValues
					.put(VocabEntity.CREATEDDATE, entity.getCreatedDate());
			inititalValues.put(VocabEntity.RIGHT, entity.getRight());
			inititalValues.put(VocabEntity.WRONG, entity.getWrong());
			boolean result = mDbAdapter.insert(VocabEntity.TABLE_NAME, null,
					inititalValues) > 0;
			return result;
		}
		return false;
	}

	public boolean deleteVocabByWordAndType(String word, String type) {
		return mDbAdapter.delete(VocabEntity.TABLE_NAME, VocabEntity.WORD
				+ "=? AND " + VocabEntity.TYPE + " =?", new String[] { word,
				type }) > 0;
	}

	public boolean deleteVocabByMean(String mean) {
		return mDbAdapter.delete(VocabEntity.TABLE_NAME, VocabEntity.MEAN
				+ "=?", new String[] { mean }) > 0;
	}

	public boolean updateVocabByWord(VocabEntity entity) {
		if (entity != null) {
			ContentValues inititalValues = new ContentValues();

			// inititalValues.put(VocabEntity.WORD, entity.getWord());
			inititalValues.put(VocabEntity.MEAN, entity.getMean());
			// inititalValues.put(VocabEntity.TYPE, entity.getType());
			inititalValues.put(VocabEntity.EXAMPLE, entity.getExample());
			inititalValues.put(VocabEntity.UNIT, entity.getUnit());
			inititalValues
					.put(VocabEntity.CREATEDDATE, entity.getCreatedDate());
			inititalValues.put(VocabEntity.RIGHT, entity.getRight());
			inititalValues.put(VocabEntity.WRONG, entity.getWrong());

			boolean result = mDbAdapter.update(VocabEntity.TABLE_NAME,
					inititalValues, VocabEntity.WORD + "=? AND "
							+ VocabEntity.TYPE + "=?",
					new String[] { entity.getWord(), entity.getType() }) > 0;

			return result;
		}
		return false;
	}

	public VocabEntity[] getAllVocab() {

		VocabEntity[] vocabs = null;
		Cursor cur = mDbAdapter.query(VocabEntity.TABLE_NAME, null, null, null,
				null, null, null);
		if (cur != null) {
			vocabs = new VocabEntity[cur.getCount()];
			int i = 0;
			while (!cur.isAfterLast()) {
				vocabs[i++] = getVocabEntity(cur);
				cur.moveToNext();
			}
			cur.close();
		}
		return vocabs;
	}

	public VocabEntity getVocabByWordAndType(String word, String type) {
		VocabEntity vocabs = null;
		Cursor cur = mDbAdapter.query(VocabEntity.TABLE_NAME, null,
				VocabEntity.WORD + "=? AND " + VocabEntity.TYPE + "=?",
				new String[] { word, type }, null, null, null);
		if (cur != null) {
			while (!cur.isAfterLast()) {
				vocabs = getVocabEntity(cur);
				cur.moveToNext();
			}
			cur.close();
		}
		return vocabs;
	}

	public VocabEntity[] getVocabByUnit(String unit) {
		VocabEntity[] vocabs = null;
		Cursor cur = mDbAdapter.query(VocabEntity.TABLE_NAME, null,
				VocabEntity.UNIT + " = ? ", new String[] { unit }, null, null,
				VocabEntity.WORD + " ASC");
		if (cur != null) {
			vocabs = new VocabEntity[cur.getCount()];
			int i = 0;
			while (!cur.isAfterLast()) {
				vocabs[i++] = getVocabEntity(cur);
				cur.moveToNext();
			}
			cur.close();
		}
		return vocabs;
	}

	public VocabEntity[] getVocabByRandom(VocabEntity entity, int quantity) {
		VocabEntity[] vocabs = null;
		Cursor cur = mDbAdapter.query(VocabEntity.TABLE_NAME, null,
				VocabEntity.WORD + " != ? AND " + VocabEntity.MEAN + "!= ?",
				new String[] { entity.getWord(), entity.getMean() }, null,
				null, "Random()LIMIT " + quantity);
		if (cur != null) {
			vocabs = new VocabEntity[cur.getCount()];
			int i = 0;
			while (!cur.isAfterLast()) {
				vocabs[i++] = getVocabEntity(cur);
				cur.moveToNext();
			}
			cur.close();
		}
		return vocabs;
	}

	public VocabEntity getVocabInUnitByRate(String unit) {
		VocabEntity[] vocabs = null;
		int count = 0;
		int[] weightHolder = null;
		int weightCounter = 0;
		int intRandom = 0;

		Cursor cur = mDbAdapter.query(VocabEntity.TABLE_NAME, null,
				VocabEntity.UNIT + " = ? ", new String[] { unit }, null, null,
				null);
		if (cur != null) {
			Log.e("ThuanPQ", "getVocabInUnitByRate not null");

			vocabs = new VocabEntity[cur.getCount()];

			while (!cur.isAfterLast()) {
				vocabs[count++] = getVocabEntity(cur);
				cur.moveToNext();
			}
			weightHolder = new int[vocabs.length];
			for (int i = 0; i < vocabs.length; i++) {
				weightHolder[i] = (int) (((double) vocabs[i].getWrong() / vocabs[i]
						.getRight()) * 1000) + weightCounter;

				weightCounter = weightHolder[i];
				Log.e("ThuanPQ",
						"weightHolder[i]: " + Integer.toString(weightHolder[i]));
				Log.e("ThuanPQ", "getVocabInUnitByRate: weightCounter"
						+ Integer.toString(weightCounter));
			}

			intRandom = (int) (Math.random() * weightCounter);

			for (int j = 0; j < weightHolder.length; j++) {
				if (weightHolder[j] >= intRandom) {
					Log.e("ThuanPQ",
							"getVocabInUnitByRate: " + vocabs[j].getWord());
					return vocabs[j];
				}
			}
			cur.close();
		}

		Log.e("ThuanPQ", "getVocabInUnitByRate null");

		return null;
	}

	private VocabEntity getVocabEntity(Cursor cur) {
		if (cur != null) {
			VocabEntity values = new VocabEntity();

			values.setWord(cur.getString(cur.getColumnIndex(VocabEntity.WORD)));
			values.setCreatedDate(cur.getString(cur
					.getColumnIndex(VocabEntity.CREATEDDATE)));
			values.setExample(cur.getString(cur
					.getColumnIndex(VocabEntity.EXAMPLE)));
			values.setMean(cur.getString(cur.getColumnIndex(VocabEntity.MEAN)));
			values.setRight(cur.getInt(cur.getColumnIndex(VocabEntity.RIGHT)));
			values.setWrong(cur.getInt(cur.getColumnIndex(VocabEntity.WRONG)));
			values.setType(cur.getString(cur.getColumnIndex(VocabEntity.TYPE)));
			values.setUnit(cur.getString(cur.getColumnIndex(VocabEntity.UNIT)));

			return values;
		}
		return null;
	}
}